<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\user_center\logic\user;
use think\facade\Hook;
use think\DB;

class Messageslogic{

	/**
     * 关注用户
     * @param  int $uid  			用户ID
     * @param  int $follow_uid    	被关注者id
     * @param  int $follow_type  	关注行为  1关注 2取关
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function focus_user($uid, $follow_uid, $follow_type){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'uid'		 	=> $uid,
                'follow_uid'   	=> $follow_uid,
                'follow_type' 	=> $follow_type,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
			if($follow_type == 1){
				//关注
				$result = model('UcMemberFollow', 'model\user') -> addData($uid, $follow_uid);
			}
			if($follow_type == 2){
				//取关
				$result = model('UcMemberFollow', 'model\user') -> deleteData($uid, $follow_uid);
			}
			if($result['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');//业务逻辑错误-1005
			}
			
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    	
    }
	
	/**
     * 绑定手机号码
     * @param  int $uid  	用户ID
     * @param  int $mobile  手机号码
     * @param  int $code  	验证码
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function bind_mobile($uid, $mobile, $code){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
                'mobile'   	=> $mobile,
                'code' 		=> $code,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//新增绑定
			$result = model('UcMemberMobile', 'model\user') -> addData($uid, $mobile);
			if($result['code'] < 0){
				DB::rollback();
				return array_out(-1005, 'error');
			}
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}

	/**
     * 校验旧手机号码以及生成token
     * @param  int $uid  	用户ID
     * @param  int $mobile  手机号码
     * @param  int $code    验证码
	 * @param  int $mobile_type 验证码类型
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function change_old_mobile($uid, $mobile , $code, $mobile_type = 2){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'			=> $uid,
                'mobile'   		=> $mobile,
                'code'   		=> $code,
                'mobile_type'   => $mobile_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			//刚刚使用的验证码
			// $result = model('UcMemberMobileSendcode', 'model\user') -> getDataByMobile($mobile, $mobile_type, 1);
			$result = model('UcMemberMobileSendcode', 'model\user') -> getDataByMobile($mobile, $mobile_type, 0);//测试阶段，不验证验证码
			
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			// 插入token
			$token = mboile_old_token($uid, $mobile, $code);
			$send_id = $result['data']->id;
			$result = model('UcMemberMobileSendcode', 'model\user') -> addDataToken($send_id, $token['mobile_token']);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			$data['mobileToken'] = (string)$token['mobile_token'];
			return array_out(200, 'success', $data);
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 新手机号码绑定
     * @param  int $uid  	用户ID
     * @param  int $mobile  手机号码
     * @param  int $code    验证码
	 * @param  int $mobile_type 	验证码类型
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function change_new_mobile($uid , $mobile , $code, $mobile_token, $mobile_type = 4){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'				=> $uid,
                'mobile'   			=> $mobile,
                'code'   			=> $code,
                'mobile_token'  	=> $mobile_token,
                'mobile_type'   	=> $mobile_type,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			//变更手机号码
			$result = model('UcMemberMobile', 'model\user') -> updateDataByMobile($uid, $mobile, $mobile_token);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 绑定微信
     * @param  int 	  $uid  	用户ID
     * @param  string $openid  
     * @param  string $unionid 
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function bind_wechat($uid, $openid, $unionid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
                'openid'   	=> $openid,
                'unionid' 	=> $unionid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//新增绑定
			$result = model('UcMemberWechat', 'model\user') -> addData($uid, $openid, $unionid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 解除绑定微信
     * @param  int 	  $uid  	用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function unbind_wechat($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		=> $uid,
            ];
			
			/********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			// 解除绑定
			$result = model('UcMemberWechat', 'model\user') -> deleteData($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 头像上传
     * @param  int 		$uid  		用户ID
     * @param  string 	$oss_url  	头像oos路径
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function avatar_upload($uid, $oss_url){
		try{
            $params = [
                'uid'			=> $uid,
                'oss_url'   	=> $oss_url,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
            return array_out(200, 'success');
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 更新用户信息
     * @param  int 		$uid  		用户ID
     * @param  string 	$nick_name  	昵称
     * @param  int 		$sex 		性别
     * @param  data 	$birthday 	生日
     * @param  string 	$avatar_url 	头像
     * @param  string 	$autograph 	个性签名
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function edit_profile($uid, $nick_name, $sex, $birthday, $autograph, $avatar_url, $title){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'			=> $uid,
                'nick_name'   	=> $nick_name,
                'sex' 			=> $sex,
                'birthday' 		=> $birthday,
                'autograph' 	=> $autograph,
                'avatar_url' 	=> $avatar_url,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

			//更新用户信息
			$result = model('Member', 'model\user') -> updateDataByUser($uid, $nick_name, $sex, $autograph, $avatar_url, $birthday, -1, $title);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback(); 
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 退出登录
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function login_out($uid){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid' => $uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            //清空登录信息
			$result = model('UcMemberAuthtoken', 'model\user') -> WipeDataByUid($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
	}
	
	
	/**
     * 粉丝列表
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function fans_list($uid){
		try{
            $params = [
                'uid' => $uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/
			
			$result = model('UcMemberFollow', 'model\user') -> getListFansByUid($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			$res = self::getFansByFollow($result['data']);
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			return array_out(200, 'success', $res);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
	}
	/**
     * 关注列表
     * @param  int $uid  		用户ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
	public function follow_list($uid){
		try{
            $params = [
                'uid'  => $uid,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            $result = model('UcMemberFollow', 'model\user') -> getListFollowByUid($uid);
			if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');
            }
			
			$res = self::getFansByFollow($result['data']);
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
			return array_out(200, 'success', $res);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
    }
    
    /**
     * 用户反馈
     * @param  int 	  $uid  	用户ID
     * @param  string $content 反馈内容  
     * @author baoshi
     * @return array_out
     */
	public function add_feedback($uid, $content){
		DB::startTrans();//启动事务
		try{
            $params = [
                'uid'		    => $uid,
                'content'   	=> $content,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('UcMemberFeedback', 'model\user') -> addData($uid, $content);
            if($rel['code'] != 200){
                DB::rollback();
                return array_out(-1005, 'error1');//前置错误 -1002
            }
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }
    
	//重组我的关注/粉丝数据
	private function getFansByFollow($result){
		$res = [];
		foreach($result as $key => $value){
			$res[$key]['userId'] 		= (int)$value['uid'];
			$res[$key]['nickName'] 		= (string)$value['nick_name'];
			$res[$key]['avatorUrl']		= (string)get_avatar_user($value['uid']);
			$res[$key]['sex'] 			= (int)$value['sex'];
			$res[$key]['autograph']		= (string)$value['autograph'];
			$res[$key]['memberStatus']	= (int)$value['member_status'];
			if($value['other'] > 0){
				$res[$key]['other'] = 1;
			}else{
				$res[$key]['other'] = 0;
			}
		}
		return $res;
	}
}